<template>
  <div class="search" v-if="isQuery">
    <el-form
      :model="searchForm"
      ref="formRef"
      :label-width="searchConfig.labelWidth ?? '80px'"
      size="large"
    >
      <el-row :gutter="20">
        <template v-for="item in searchConfig.formItems" :key="item.prop">
          <el-col :span="8">
            <el-form-item :label="item.label" :prop="item.prop">
              <template v-if="item.type === 'input'">
                <el-input
                  v-model="searchForm[item.prop]"
                  :placeholder="item.placeholder"
                >
                </el-input>
              </template>
              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  v-model="searchForm[item.prop]"
                />
              </template>
              <template v-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  v-model="searchForm[item.prop]"
                >
                  <template v-for="option in item.options" :key="option.value">
                    <el-option
                      :label="option.label"
                      :value="option.value"
                    ></el-option>
                  </template>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <!-- 重置和搜索 -->
    <div class="btn">
      <el-button @click="handleResetClick" icon="Refresh" size="large"
        >重置</el-button
      >
      <el-button
        @click="handleQueryClick"
        icon="Search"
        size="large"
        type="primary"
        >查询</el-button
      >
    </div>
  </div>
</template>
<script setup lang="ts">
import usePermissions from '@/hooks/usePermissons'
import type { ElForm } from 'element-plus'
import { reactive, ref } from 'vue'
interface IProps {
  searchConfig: {
    pageName: string
    labelWidth?: string
    formItems: any[]
  }
}
const props = defineProps<IProps>()
// 权限
const isQuery = usePermissions(`${props.searchConfig.pageName}:query`)
const emit = defineEmits(['queryClick', 'resetClick'])
// 定义form数据初始化
const initialForm: any = {}
// 字段名和服务器一致
for (const item of props.searchConfig.formItems) {
  initialForm[item.prop] = item.initialVlaue ?? ''
}
const searchForm = reactive(initialForm)
// 重置 拿到实例对象
const formRef = ref<InstanceType<typeof ElForm>>()
function handleResetClick() {
  // searchForm.name = '' 手动重置或者调用方法。
  formRef.value?.resetFields()
  emit('resetClick')
}
// 发送给父组件事件

// 查询
function handleQueryClick() {
  // console.log('查询', searchForm)
  emit('queryClick', searchForm)
}
</script>
<style lang="less" scoped>
.search {
  background-color: #fff;
  padding: 20px;
  .el-form-item {
    padding: 10px;
    margin-bottom: 0;
  }
  .btn {
    text-align: right;
    .el-button {
      height: 36px;
    }
  }
}
</style>
